﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BackgroundTask {
    /// <summary>
    /// An event argument that contains a result of a return value from a runned task.
    /// </summary>
    /// <typeparam name="TResult">The returning type.</typeparam>
    public class ActionDoneEventArgs<TResult> : EventArgs {
        /// <summary>
        /// Gets the Result.
        /// </summary>
        public TResult Result { get; private set; }

        /// <summary>
        /// Gets an Exception that was thrown by the action.
        /// Is null when no Exception was thrown.
        /// </summary>
        public Exception Exception { get; private set; }

        /// <summary>
        /// Indicates whether an Exception was thrown or not.
        /// </summary>
        public bool IsFailed { 
            get {
                return Exception != null;
            } 
        }

        /// <summary>
        /// Gets the underlaying action
        /// </summary>
        public object Action { get; private set; }

        /// <summary>
        /// Initializes a new ActionDoneEventArgs-Object with a given return value.
        /// </summary>
        /// <param name="result">The returned result.</param>
        public ActionDoneEventArgs(TResult result) {
            Result = result;
        }

        /// <summary>
        /// Initializes a new ActionDoneEventArgs-Object with a given return value.
        /// </summary>
        /// <param name="result">The returned result.</param>
        /// <param name="action">The action.</param>
        public ActionDoneEventArgs (TResult result, object action) {
            Result = result;
            Action = action;
        }

        /// <summary>
        /// Initializes a new ActionDoneEventArgs-Object with an Exception.
        /// Will be called when an action was done with throwing an Exception.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="ex">An Exception-Object that was thrown.</param>
        public ActionDoneEventArgs (object action, Exception ex) {
            Result = default(TResult);
            Action = action;
            Exception = ex;
        }
    }
}
